<?php
// +----------------------------------------------------------------------
// | HkCms 验证码处理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\api\controller;

use common\controller\Hkcms;

class Checkcode extends Hkcms
{
    /**
     * 生成验证码
     */
    public function index()
    {
        $checkcode = new \libs\util\Checkcode();
        //验证码类型
        $checkcode->type = input('type', 'verify', 'strtolower');
        //设置长度
        $codelen = input('code_len', 0, 'intval');
        if ($codelen) {
            if ($codelen > 8 || $codelen < 2) {
                $codelen = 4;
            }
            $checkcode->codelen = $codelen;
        }
        //设置验证码字体大小
        $fontsize = input('font_size', 0, 'intval');
        if ($fontsize) {
            $checkcode->fontsize = $fontsize;
        }
        //设置验证码图片宽度
        $width = input('width', 0, 'intval');
        if ($width) {
            $checkcode->width = $width;
        }
        //设置验证码图片高度
        $height = input('height', 0, 'intval');
        if ($height) {
            $checkcode->height = $height;
        }
        //设置背景颜色
        $background = input('background', '', '');
        if ($background) {
            $checkcode->background = $background;
        }
        //设置字体颜色
        $fontcolor = input('font_color', '', '');
        if ($fontcolor) {
            $checkcode->fontcolor = $fontcolor;
        }

        //显示验证码
        $checkcode->output(input('refresh', false, ''));
    }

    /**
     * 验证输入，看它是否生成的代码相匹配。
     * @param $type
     * @param $input
     * @return bool
     */
    public function validateCode($type, $input)
    {
        $checkcode = new \libs\util\Checkcode();
        $checkcode->type = $type;
        return $checkcode->validate($input, false);
    }
}
